//
//  ProductModel.m
//  DA_V
//
//  Created by 中亿世联前台 on 15/5/13.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "ProductModel.h"
#import "AFNetworking.h"
#import "InterFace.h"
#import "EncryptionData.h"

@implementation ProductModel
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    

    
//    value = [NSString stringWithFormat:@"%@",value];
//    if ([key isEqualToString:@"ProductID"]) {
//        self.productID = value;
//    }else if ([key isEqualToString:@"ThumbPath"]){
//        self.thumbPath = value;
//    } else if([key isEqualToString:@"ProductName"]){
//        self.productName = value;
//    }
    
}

- (void)getProductList:(NSMutableArray *)array{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    [manager POST:ProductInterface parameters:NULL success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *str = responseObject[@"sign"];
        @try {
            
            EncryptionData *data = [[EncryptionData alloc]init];
            NSString *datastr = [data decryption:str :PasswordKey];
            
            NSData* dataFromString = [datastr dataUsingEncoding:NSUTF8StringEncoding];
            
            NSDictionary *dataDic = [NSJSONSerialization  JSONObjectWithData:dataFromString options:NSJSONReadingMutableContainers error:nil];
            
            //返回成功
            if ( [[dataDic objectForKey:@"status"] isEqualToString:@"success"]) {
                
                NSArray *myDatArr =dataDic[@"data"];
                
                for (int i = 0; i <[myDatArr count]; i++) {
                    
                    NSDictionary *productDic = myDatArr[i];
                    ProductModel *model = [[ProductModel alloc] init];
                    
                    [model setValuesForKeysWithDictionary:productDic];
                    
                    [array addObject:model];
                }
                [self.productModelDelegate getgetProductListEnd];
            }   else if ( [[dataDic objectForKey:@"status"] isEqualToString:@"failed"]) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[dataDic objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            } else{
                [self.productModelDelegate getError];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"服务器异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }

        }
        @catch (NSException *exception) {
            [self.productModelDelegate getError];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"服务器异常" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
   
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.productModelDelegate getError];
        NSLog(@"＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝ %@", [error description]);
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
    }];
    
}
@end
